
<template>
  <div class="container-article">
    <!-- 搜索区域 -->
    <el-card>
      <!-- 头部 -->
      <div slot="header">
        <!-- 自己封装的组件实现 -->
        <my-bread>内容管理</my-bread>
      </div>
      <!-- 内容 -->
      <el-form label-width="80px" size="small">
        <el-form-item label="状态：">
          <el-radio-group v-model="reqParams.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 频道 -->
        <el-form-item label="频道：">
          <!-- 使用my-channel组件 -->
          <!-- :value="reqParams.channel_id" @input="reqParams.channel_id=$event" v-model语法糖 reqParams.channel_id-->
          <my-channel :value="reqParams.channel_id" @input="reqParams.channel_id=$event"></my-channel>
        </el-form-item>
        <el-form-item label="日期：">
          <!-- 组件产生两个数据，是数组收集数据，[开始日期,结束日期] -->
          <el-date-picker
            @change="changeDate"
            value-format="yyyy-MM-dd"
            v-model="dateArr"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search()">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 结果区域 -->
    <el-card style="margin-top:20px">
      <div slot="header">根据筛选条件共查询到 {{total}} 条结果：</div>
      <!-- 表格 -->
      <el-table :data="articles">
        <el-table-column label="封面">
          <template slot-scope="scope">
            <el-image :src="scope.row.cover.images[0]" style="width:180px;height:120px">
              <div slot="error" class="image-slot">
                <img src="../../assets/error.gif" alt style="width:180px;height:120px" />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title" width="400px"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status===0" type="info">草稿</el-tag>
            <el-tag v-if="scope.row.status===1">待审核</el-tag>
            <el-tag v-if="scope.row.status===2" type="success">审核通过</el-tag>
            <el-tag v-if="scope.row.status===3" type="warning">审核失败</el-tag>
            <el-tag v-if="scope.row.status===4" type="danger">已删除</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="发布时间" prop="pubdate"></el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button
              @click="toEdit(scope.row.id)"
              type="primary"
              icon="el-icon-edit"
              circle
              plain
            ></el-button>
            <el-button
              @click="delArticle(scope.row.id)"
              type="danger"
              icon="el-icon-delete"
              circle
              plain
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="reqParams.per_page"
        :current-page="reqParams.page"
        @current-change="changePager"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  //变量名和属性名相同  MyBread:MyBread

  name: "page-article",
  data() {
    return {
      reqParams: {
        status: null,
        //默认查询全部
        channel_id: null,
        //日期范围开始
        begin_pubdate: null,
        //日期范围结束
        end_pubdate: null,
        //页码
        page: 1,
        //每页条数
        per_page: 20,
      },
      total: 0,
      dateArr: [],
      articles: [],
      channelOptions: [],
    };
  },
  created() {
    this.getArticles();
  },
  methods: {
    //删除文章
    delArticle(id) {
      this.$confirm("此操作将永久删除该文章, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          //点击确认,发删除请求
          try {
            //1.测试账户的数据,后台限制不能删除
            //2.请求成功 ,后台没有响应内容
            await this.$http.delete(`articles/${id}`);
            this.$message.success("删除文章成功");
            this.getArticles();
          } catch (e) {
            this.$message.error("删除文章失败");
          }
        })
        .catch(() => {});
    },
    //编辑文章

    toEdit(id) {
      this.$router.push({ path: "/publish", query: { id } });
    },

    //筛选函数
    search() {
      this.reqParams.page = 1;
      this.getArticles();
    },
    changeDate(arr) {
      //arr是数组
      //组件可以设置格式
      //注意:清空日期的时候也会触发该函数
      if (arr) {
        this.reqParams.begin_pubdate = arr[0];
        this.reqParams.end_pubdate = arr[1];
      } else {
        this.reqParams.begin_pubdate = null;
        this.reqParams.end_pubdate = null;
      }
    },
    //分页的切换
    //el-pagination 绑定change事件
    changePager(newPage) {
      //把新页码传给服务器
      this.reqParams.page = newPage;
      //在更新页面
      this.getArticles();
    },

    //获取文章数据
    async getArticles() {
      //给后台传参 get 后写两个参数  第二个{params:this.res.Params}把我们筛选的参数传给后台

      const res = await this.$http.get("articles", { params: this.reqParams });

      this.articles = res.data.data.results;

      this.total = res.data.data.total_count;
    },
  },
};
</script>

<style>
</style>